package com.yusvn.scm.common;

import com.alibaba.fastjson.util.TypeUtils;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yusvn.scm.common.constant.Const;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 基础业务类
 * @author liugz
 *
 */
public class BaseService {

	/**
	 * 返回第几页，默认1
	 * @param kv
	 * @return int
	 */
	public int getPage(Kv kv) {
		return TypeUtils.castToInt( getKvValue(kv, "page", 1) );
	}
	
	/**
	 * 返回每页行数，默认3000
	 * @param kv
	 * @return int
	 */
	public int getLimit(Kv kv) {
		return TypeUtils.castToInt( getKvValue(kv, "limit", 3000) );
	}

	/**
	 * 获取Kv值，无数据则取默认值
	 * @param kv
	 * @param name
	 * @param defaultValue
	 * @return
	 */
	public Object getKvValue(Kv kv, String name, Object defaultValue) {
//		if(kv.isEmpty()) {
//			throw new RuntimeException("kv cannot be null");
//		}
//		kv.set(name, defaultValue);
		
		Object obj = kv.notNull(name) ? kv.get(name) : defaultValue;
		return obj;
	}

	/**
	 * 根据ERP用户编号 获取登录用户对应的ERP用户的品牌权限
	 * @param erpUsercode ERP用户编号
	 * @return
	 */
	public List<String> getPpqx(String erpUsercode) {
		return Db.use(Const.SCMDB).query("SELECT brandid FROM DEFBRANDPERS "
				+ "WHERE userid=(select id from SYSUSER where cid=? and usercode=?)", getCid(), erpUsercode);
	}

	/**
	 * 根据ERP用户编号 获取登录用户对应的ERP用户的所属仓店权限
	 * @param erpUsercode ERP用户编号
	 * @return
	 */
	public String getSsmdqx(String erpUsercode) {
		return Db.use(Const.SCMDB).queryStr("select storeid from SYSUSER where cid=? and usercode=? ", getCid(), erpUsercode);
	}
	
	/**
	 * 根据ERP用户编号 获取登录用户对应的ERP用户的主仓店权限
	 * @param erpUsercode ERP用户编号
	 * @return
	 */
	public List<String> getZcmdqx(String erpUsercode) {
		return Db.use(Const.SCMDB).query("select b.storeid from SYSUSER a,DEFSTOREPERS b "  
				+ "where a.id=b.userid and a.cid=? and a.usercode=? and CANEDIT=1", getCid(), erpUsercode);
	}
	
	/**
	 * 获取商品图片地址
	 * @return
	 */
	public String getGoodsimg() {
		String goodsimg = Db.use(Const.SCMDB).queryStr(""
				+ "SELECT pz1 FROM scm_pz WHERE bh='goodsimg' ");
//		String goodsimg = PropKit.get("goodsimg"); //从配置文件中获取图片地址
		
		return goodsimg;
	}
	
	/**
	 * 获取配置文件中设置的账套id（eboss的cid）  sysuser用户表、syscompany账套表、
	 * @return
	 */
	public String getCid() {
		String cid = Db.use(Const.SCMDB).queryStr(""
				+ "select id from syscompany where companycode=(SELECT pz1 FROM scm_pz WHERE bh='suit_code') ");
		if(StrKit.isBlank(cid)) {
			cid = "1";
		}
//		String cid = PropKit.get("cid"); //获取账套编号
		return cid;
	}
		
	/**
	 * 获取SCM参数值
	 * @param configCode参数标识
	 * @return
	 */
	public String getConfig(String configCode) {
		return Db.use().queryStr("select config_value1 from scm_config where config_code=? ", configCode);
	}
	
	/**
	 * 获取SCM参数值
	 * @param configCode参数标识
	 * @param col第几列的值
	 * @return
	 */
	public String getConfig(String configCode, String col) {
		return Db.use(Const.SCMDB).queryStr("select pz"+col+" from scm_pzfh where bh=? ", configCode);
	}

	/**
	 *
	 * 查询动态字段
	 * @return
	 */
	public Map<String,String> selectPzFh(){
		Map<String,String> map = new HashMap<>();
		List<Record> recordList = Db.use(Const.SCMDB).find("select * from scm_pzmx where mid in (select id from SCM_PZ where bh = 'scm_sdfh_dtzd')");
		for (Record record : recordList) {
			map.put(record.get("pzmx1"),record.get("pzmx2"));
		}

		return map;
	}

	/**
	 *
	 * 分货查询动态字段
	 * @return
	 */
	public Map<String,String> selectPz(){
		Map<String,String> map = new HashMap<>();
		List<Record> recordList = Db.use(Const.SCMDB).find("select * from scm_pzbhmx where mid in (select id from SCM_PZBH where bh = 'scm_buhuo_dtzd')");
		for (Record record : recordList) {
			map.put(record.get("pzmx1"),record.get("pzmx2"));
		}

		return map;
	}

	/**
	 *
	 * 查询手动分货动态字段
	 * @return
	 */
	public Map<String,String> selectSdfhPz(){
		Map<String,String> map = new HashMap<>();
		List<Record> recordList = Db.use(Const.SCMDB).find("select * from scm_pzmx where mid in (select id from SCM_PZ where bh = 'scm_sdfh_dtzd')");
		for (Record record : recordList) {
			map.put(record.get("pzmx1"),record.get("pzmx2"));
		}

		return map;
	}
	
}
